package org.example.controller.market;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.annotation.HasRole;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.entity.market.CBusiness;
import org.example.service.market.BusinessService;
import org.example.skytool.commom.ParamUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/market")
@CrossOrigin
public class BusinessController {

    @Resource
    private BusinessService businessService;

    /**
     * 新增商户
     */
    @PostMapping("/addBusiness")
    @HasRole(role = "管理员")
    public Result addBusiness(@RequestBody CBusiness business) {
        business = businessService.addBusiness(business);
        return new Result(ResultCode.SUCCESS, business);
    }

    /**
     * 删除商户
     */
    @PostMapping("/deleteBusiness")
    @HasRole(role = "管理员")
    public Result deleteBusiness(@RequestBody CBusiness business) {
        Long id = business.getId();
        ParamUtil.LongParam(id);
        businessService.deleteBusiness(id);
        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 修改商户名
     */
    @PostMapping("/updateName")
    @HasRole(role = "商户")
    public Result updateName(@RequestBody CBusiness business) {
        String name = business.getName();
        ParamUtil.StringParam(name);
        business = businessService.updateName(name);
        return new Result(ResultCode.SUCCESS, business);
    }

    /**
     * 修改商户电话
     */
    @PostMapping("/updatePhone")
    @HasRole(role = "商户")
    public Result updatePhone(@RequestBody CBusiness business) {
        String phone = business.getPhone();
        ParamUtil.LongParam(phone);
        business = businessService.updatePhone(phone);
        return new Result(ResultCode.SUCCESS, business);
    }

    /**
     * 修改商户信息（管理）
     */
    @PostMapping("/updateBusiness")
    @HasRole(role = "管理员")
    public Result updateBusiness(@RequestBody CBusiness business) {
        ParamUtil.LongParam(business.getId());
        business = businessService.updateInfo(business);
        return new Result(ResultCode.SUCCESS, business);
    }

    /**
     * 查询商户信息
     */
    @PostMapping("selectBusiness/{limit}/{start}")
    @HasRole(role = "管理员")
    public Result selectBusiness(
            @PathVariable("limit") Long limit,
            @PathVariable("start") Long start
    ) {
        IPage<CBusiness> page = businessService.getBusiness(limit, start);
        return new Result(ResultCode.SUCCESS, page);
    }

}
